var is_upload=true;
$(function() {
    $('.IndexUser').click(function(e){
        location.href=getRootPath()+'/GasHome/Mine/Index?park_id='+$('#park_id').val();
    });



    var SubMit = document.querySelector('.SubMit')
    var modle1 = document.querySelector('.Model1')
    SubMit.index = true
    // SubMit.addEventListener('click', function() {
    //     if (SubMit.index) {
    //         modle1.className = 'Model Model-active'
    //         SubMit.index = !SubMit.index
    //     } else {
    //         return false
    //     }
    // })
    modle1.addEventListener('click', function() {
        if (!SubMit.index) {
            modle1.className = 'Model Model1'
            SubMit.index = !SubMit.index
            window.location.reload();
        } else {
            return false
        }
    })

    var IpIntegralRule=document.querySelector('.IpIntegralRule')
    var modle2=document.querySelector('.Model2')
    IpIntegralRule.index=true
    IpIntegralRule.addEventListener('click',function(e){
        e.stopPropagation();
        if(IpIntegralRule.index){
            modle2.className='Model Model2 Model-active'
            IpIntegralRule.index=!IpIntegralRule.index
        }else{
            return false
        }
    })
    modle2.addEventListener('click',function(){
        if(!IpIntegralRule.index){
            modle2.className='Model Model2'
            IpIntegralRule.index=!IpIntegralRule.index
        }else{
            return false
        }
    })

    //时间
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = Number(myDate.getMonth()) + 1;
    var day = myDate.getDate();
    var select1 = $.selectDate(".data-line", {
        start: 1900,
        end: 3000,
        select: [year, month, day],
        title: '开始日期'
    }, function(data) {
        $(".date").val(data.year + "-" + data.month + "-" + data.day);
        $(".date-label").html(data.year + "-" + data.month + "-" + data.day);
    });
    $(".img1").click(function(){
        select1.show();
    });

    //图片上传
    // $(".info-img-item img").click(function() {
    //     $(this).next().click();
    // });
    $("input[type='file']").change(function() {
        uploadFile(this);
    });

    $('.add-other').click(function(){
        $('.other-product-box').append($('.other-openShop-demo').html());
    });

    //提交
    $('.SubMit').click(function(){
        var load_alert=loadAlert('提交中...');
        $.ajax({
            type: "post",
            url: "Api/QrcodeApply/saveQrcodeApply",
            data:$('#qrcode-form').serialize(),
            async: true,
            dataType: 'json',
            success: function(data) {
                closeAlert(load_alert);
                if (data.status) {
                    if (SubMit.index) {
                        modle1.className = 'Model Model-active'
                        SubMit.index = !SubMit.index
                    } else {
                        return false
                    }
                } else {
                    errorAlert(data.msg);
                }
            }
        });
    });

    $('.input-li').click(function(){
        if($(this).find('input').length>0){
            $(this).find('input').eq(0).focus();
        }
    });

    $('.product-box').click(function(e){
        return false;
    });
    $('.other-product-box').click(function(e){
        return false;
    });
})

function doJian(obj) {
    if ($(obj).next().val() > 1) {
        $(obj).next().val(Number($(obj).next().val()) - 1);
    }
    else{
       $(obj).parents('.openShop').remove(); 
    }
}

function doJia(obj) {
    $(obj).prev().val(Number($(obj).prev().val()) + 1);
}

function showProductIframe(){
    $('.product-frame').show();
    $('.product-frame iframe').animate({top:'0%'});
}



var now_type = '';

function uploadDocument(obj) {
    if(is_upload){
        now_type = $(obj).attr('data-type');
        $(obj).next().click();
    }
}

function uploadFile(obj) {
    var fileObj = $(obj).get(0).files; // js 获取文件对象
    if(fileObj.length==0){
        errorAlert('请选择图片');
        return;
    }
    var formFile = new FormData();
    for (var i = 0; i <fileObj.length; i++) {
        formFile.append("myfile[]", fileObj[i]); //加入文件对象
    }
    var data = formFile;

    $('.document-p .progress-bar').css('width', '0%');
    $('.document-p .p-text').html('已上传（0%）');
    $('.document-p').show();
    $('.add-img').removeAttr('onclick');
    is_upload=false;
    // $('.zhezhao').show();
    setTimeout(function() {
        $.ajax({
            url: 'Admin/CommonUpload/upload/' + now_type,
            data: data,
            type: "POST",
            dataType: "json",
            cache: false, //上传文件无需缓存
            processData: false, //用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            xhr: xhrOnProgress(function(e) {
                var percent = e.loaded / e.total;
                var now_jindu = (parseFloat(percent) * 100).toFixed(2);
                console.log(now_jindu);
                $('.document-p .progress-bar').css('width', now_jindu + '%');
                $('.document-p .p-text').html('已上传（' + now_jindu + '%）');
                $('.document-p').show();
            }),
            success: function(data) {
                is_upload=true;
                $('.add-img').attr('onclick','uploadDocument(this)');
                $('.document-p .progress-bar').css('width', '100%');
                $('.document-p .p-text').html('已上传（100%）');
                $('.document-p').hide();
                if (data.status) {
                    // alert(now_type);
                    var path=JSON.parse(data.v);
                    var icon=JSON.parse(data.remark);
                    
                    $(obj).prev().prev().val(path);
                    $(obj).prev().attr('src',path);
                    $(obj).next().hide();
                } else {
                    errorAlert(data.msg);
                }
            }
        })
    }, 1000);
}

var xhrOnProgress = function(fun) {
    xhrOnProgress.onprogress = fun; //绑定监听
    //使用闭包实现监听绑
    return function() {
        //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
        var xhr = $.ajaxSettings.xhr();
        //判断监听函数是否为函数
        if (typeof xhrOnProgress.onprogress !== 'function')
            return xhr;
        //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
        if (xhrOnProgress.onprogress && xhr.upload) {
            xhr.upload.onprogress = xhrOnProgress.onprogress;
        }
        return xhr;
    }
}